//
//  ClubMyCommitCell.swift
//  77Club
//
//  Created by Apple on 2024/9/18.
//

import UIKit
import Cosmos

class ClubMyCommitCell: UITableViewCell {
    
    private let bgView = c_make_ex(UIView.self)
        .bgColor(.white.alpha(0.9)).corner(12.0.Scale).end()
    private let iconImageView = c_make_ex(UIImageView.self)
        .corner(20.0.Scale).mode(.scaleAspectFill).end()
    private let nameLabel = c_make_ex(UILabel.self)
        .color(.black).font(12.font_semiBold).end()
    private let timeLabel = c_make_ex(UILabel.self)
        .color("#AEB0B3").font(11.font_medium).end()
    private let contentLabel = c_make_ex(UILabel.self)
        .color("#47494D").font(15.font_medium)
        .line(0).end()
    private var starView = CosmosView()
    private let gameView = c_make_ex(UIView.self)
        .corner(8).bgColor("#47494D").end()
    private let gameIcon = c_make_ex(UIImageView.self)
        .corner(8).mode(.scaleAspectFill).end()
    private let gameName = c_make_ex(UILabel.self)
        .color(.white).font(12.font_semiBold).end()
    private let gameSubLabel = c_make_ex(UILabel.self)
        .color(.white.alpha(0.85)).font(10.font_medium).end()
    private let moreButton = c_make_ex(UIButton.self)
        .image("Mine_Commit_more").end()
    
    var toGameDetailBlock: ((_ id: String) -> Void)?
    var delCommitBlock: ((_ id: String) -> Void)?
    
    var model: ClubMyCommitListModel? {
        didSet{
            guard let md = model else { return }
            
            iconImageView.loadImage(url: ClubUser.face)
            contentLabel.text = md.content
            nameLabel.text = ClubUser.user_name
            timeLabel.text = md.time + " 评论了游戏"
            starView.rating = md.point
            gameIcon.loadImage(url: md.icon)
            gameName.text = md.game
            let tagStr = md.biaoqian.joined(separator: " · ")
            gameSubLabel.text = "\(md.game_point)" + " · " + tagStr
        }
        
    }
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        initUI()
    }
    
    func initUI() -> Void {
        selectionStyle = UITableViewCell.SelectionStyle.none
        backgroundColor = UIColor.clear
        
        contentView.subviews{
            bgView.subviews{
                iconImageView
                nameLabel
                timeLabel
                contentLabel
                starView
                gameView.subviews{
                    gameIcon
                    gameName
                    gameSubLabel
                }
                moreButton
            }
        }
        
        bgView.leading(12.0.Scale).trailing(12.0.Scale).top(0).bottom(12.0.Scale)
        iconImageView.leading(12.0.Scale).top(16.0.Scale).size(40.0.Scale)
        nameLabel.leading(64.0.Scale).top(18.0.Scale).trailing(15.0.Scale)
        timeLabel.leading(64.0.Scale).top(36.0.Scale).trailing(15.0.Scale)
        starView.leading(64.0.Scale).top(54.0.Scale)
        contentLabel.leading(64.0.Scale).top(73.0.Scale).trailing(16.0.Scale)
        gameView.leading(64.0.Scale).trailing(16.0.Scale).height(56).bottom(12.0.Scale)
        gameView.Top == contentLabel.Bottom + 8.0.Scale
        gameIcon.leading(12.0.Scale).size(32.0.Scale).centerVertically()
        gameName.leading(56.0.Scale).top(12.0.Scale).trailing(20.0.Scale)
        gameSubLabel.leading(56.0.Scale).trailing(16.0.Scale).bottom(10.0.Scale)
        moreButton.trailing(8).size(32)
        moreButton.CenterY == iconImageView.CenterY
        
        starView.rating = 0
        starView.settings.starSize = 12.0.Scale
        starView.settings.starMargin = 5
        starView.settings.fillMode = .half
        starView.settings.emptyImage = "Common_emptyStar".toImage
        starView.settings.filledImage = "Common_fillStar".toImage
        starView.isUserInteractionEnabled = false
        
        let tapG = UITapGestureRecognizer(target: self, action: #selector(clickGameView))
        gameView.isUserInteractionEnabled = true
        gameView.addGestureRecognizer(tapG)
     
        moreButton.addTarget(self, action: #selector(clickMoreButton), for: .touchUpInside)
    }
    
    @objc private func clickGameView(){
        toGameDetailBlock?(model?.gid ?? "")
    }

    @objc private func clickMoreButton(){
        delCommitBlock?(model?.id ?? "")
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }
    
//    @objc func buttonClick(_ button:UIButton) -> Void {
//        photoBroseView.imagesURL = model?.images
//        photoBroseView.frameFormWindow = CGRect(x: screenWidth/2, y: screenHeight/2, width: 0, height: 0)
//        photoBroseView.frameToWindow = CGRect(x: screenWidth/2, y: screenHeight/2, width: 0, height: 0)
//        photoBroseView.currentIndex = button.tag - 9999
//        photoBroseView.show()
//    }
//
//    @objc func zanButtonClick(_ button:UIButton) -> Void {
//        self.zanButtonClick?(model?.id ?? "")
//    }
//
//    @objc func videoButtonClick(_ button:UIButton) -> Void {
//        self.videoPlayClick?(self.model?.video ?? "")
//    }

}

class ClubMyCommitMoreView: UIView, UITableViewDelegate, UITableViewDataSource, RaeAnimationProtocol, RaeAnimationDelegate {

    func animationContainerViewSize() -> CGSize {
        CGSize(width: .screenWidth, height: 150)
    }
    
    func animationContainerViewStyle() -> RaeAnimationViewStyle {
        .bottom
    }
    
    private lazy var tableView: UITableView = {
        let tableView = UITableView()
        tableView.delegate = self
        tableView.dataSource = self
        tableView.backgroundColor = .white
        tableView.showsVerticalScrollIndicator = false
        tableView.register(ClubMoreItemCell.self)
        tableView.contentInsetAdjustmentBehavior = .never
        tableView.separatorStyle = .none
        tableView.rowHeight = 45.0.Scale
        tableView.layer.cornerRadius = 8.0.Scale
        tableView.layer.masksToBounds = true
        return tableView
    }()
    private let cancelButton = c_make_ex(UIButton.self)
        .corner(8.0.Scale).bgColor(.white).text("取消")
        .font(15.font_semiBold).color("#2E3033").end()
    
    var commitID = "" {
        didSet{
            loadData()
        }
    }
    
    private var models = [ClubMoreModel]()
    
    var delBlock: (() -> Void)?
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        backgroundColor = "#E8E9EB".toColor
        rae.corner(12.0.Scale, corners: [.layerMinXMinYCorner,.layerMaxXMinYCorner])
        
        subviews{
            tableView
            cancelButton
        }
        
        tableView.leading(16.0.Scale).trailing(16.0.Scale).top(20.0.Scale).height(45.0.Scale)
        cancelButton.leading(16.0.Scale).trailing(16.0.Scale).height(45.0.Scale)
        cancelButton.Top == tableView.Bottom + 9.0.Scale
        
        cancelButton.addTarget(self, action: #selector(clickCancelButton), for: .touchUpInside)
    }
    
    private func loadData(){
        
        let delModel = ClubMoreModel(title: "删除评论", img: "Home_more_del", isRed: true)
        models.append(delModel)
        
        self.tableView.reloadData()
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

        return models.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        let cell = tableView.dequeueReusableCell(withIdentifier: "ClubMoreItemCell", for: indexPath) as! ClubMoreItemCell
        cell.model = models[indexPath.row]
        return cell
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        
        entryAnimate.animation(dismiss: self){
            self.delBlock?()
        }
        
    }
    
    @objc private func clickCancelButton(){
        entryAnimate.animation(dismiss: self)
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}
